﻿Imports Aricie.DNN.Services
Imports Aricie.DNN.Services.UpgradeSystem
Imports Aricie.DNN.Services.UpgradeSystem.Actions.Settings
'Imports Aricie.DNN.Configuration
Imports Aricie.DNN.Modules.LuceneSearch.Business
'Imports Aricie.DNN.Modules.LuceneSearch
Imports System.IO
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Services.Packages
Imports System.Reflection


Namespace UpgradeSystem
    Public Class UpgradeTo180ActionsProvider
        Implements IUpgradeActionsProvider(Of LuceneSearchController)

        Public Function GetActions() As UpgradeActionsCollection Implements IUpgradeActionsProvider(Of LuceneSearchController).GetActions
            Dim UpgradeTo180 = New UpgradeActionsCollection(New Version(1, 8, 0))

            UpgradeTo180.GlobalUpgradeActions.Add(New Aricie.DNN.Services.UpgradeSystem.Actions.GlobalUpgradeAction(Actions.ActionStep.MigrationEnded) With {.Name = "We add a _false_ entry for Lucene.Net under the search package for DNN in order for our uninstall not to delete the assembly", .UpgradeOperation = AddressOf AddingFalseAssemblyRegistrationToDNN71})

            Return UpgradeTo180
        End Function

        Private Sub AddingFalseAssemblyRegistrationToDNN71(TargetVersion As Version)
            ' let's copy Lucene.Net to the bin directory if DNN version is lower than 7.1
            If (NukeHelper.DnnVersion >= New Version(7, 1, 0)) Then

                Try
                    Dim SafePackageNameForLuceneSearch As String = "LUCENE.NET SAVED LIBRARY"



                    Dim TypesInAssembly As IEnumerable(Of Type) = AppDomain.CurrentDomain.GetAssemblies.Where(Function(a) a.CodeBase.ToLower().Contains("dotnetnuke.dll")).First().GetTypes 'Assembly.GetExecutingAssembly().GetTypes()
                    Dim PackageControllerType As Type = TypesInAssembly.Where(Function(a) a.Name = "PackageController").First()
                    Dim PackageInfoType As Type = TypesInAssembly.Where(Function(a) a.Name = "PackageInfo").First()

                    Dim PackageInfoInstance As Object = Activator.CreateInstance(PackageInfoType)

                    PackageInfoType.GetProperty("Description").SetValue(PackageInfoInstance, "Lucene.Net save package to block erroneous uninstall", Nothing)
                    PackageInfoType.GetProperty("FriendlyName").SetValue(PackageInfoInstance, "Lucene.Net save package to block erroneous uninstall", Nothing)
                    PackageInfoType.GetProperty("Name").SetValue(PackageInfoInstance, SafePackageNameForLuceneSearch, Nothing)
                    PackageInfoType.GetProperty("Version").SetValue(PackageInfoInstance, New Version(1, 0, 0), Nothing)
                    PackageInfoType.GetProperty("PackageType").SetValue(PackageInfoInstance, "Library", Nothing)

                    Dim PackageId As Integer = Convert.ToInt32(PackageControllerType.GetMethod("AddPackage").Invoke(Nothing, New Object() {PackageInfoInstance, False}))

                    DotNetNuke.Data.DataProvider.Instance.RegisterAssembly(PackageId, "Lucene.Net.dll", "3.0.3")
                    
                Catch ex As Exception
                    Aricie.Services.ExceptionHelper.LogException(ex)
                End Try

            
            End If
        End Sub


    End Class
End Namespace